﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SYcwpb.Models;

namespace SYcwpb.ViewModels.DataProvider
{
    public delegate void ItemEndEditEventHandler(IEditableObject sender);

    class AttendanceObjects:ObservableCollection<AttendanceObject>
    {
        public event ItemEndEditEventHandler itemEndEdit;

        protected override void InsertItem(int index, AttendanceObject item)
        {
            base.InsertItem(index, item);

            item.ItemEndEdit += new ItemEndEditEventHandler(item_ItemEndEdit);
        }

        void item_ItemEndEdit(IEditableObject sender)
        {
            if (itemEndEdit != null)
            {
                itemEndEdit(sender);
            }
        }
    }

    class AttendanceObject : IEditableObject, INotifyPropertyChanged
    {
        private DriverAttendanceOrderAdjustModel _model = DriverAttendanceOrderAdjustModel.CreateDriverAttendanceOrderAdjustModel();

        public event ItemEndEditEventHandler ItemEndEdit;

        public void BeginEdit() { }

        public void CancelEdit() { }

        public void EndEdit()
        {
            if (ItemEndEdit != null)
                ItemEndEdit(this);
        }

        public AttendanceObject(DriverAttendanceOrderAdjustModel model)
        {
            this._model = model;
        }

        public DriverAttendanceOrderAdjustModel GetModel()
        {
            return _model;
        }

        public string ID
        {
            get { return _model.ID; }
            set
            {
                if (value == _model.ID)
                    return;
                _model.ID = value;
                OnPropertyChanged("ID");
            } 
        }

        public string UserID
        {
            get { return _model.UserID; }
            set
            {
                if (value == _model.UserID)
                    return;
                _model.UserID = value;
                OnPropertyChanged("UserID");
            }
        }

        public string Name
        {
            get { return _model.Name; }
            set
            {
                if (value == _model.Name)
                    return;
                _model.Name = value;
                OnPropertyChanged("Name");
            }
        }
        public string Group
        {
            get { return _model.Group; }
            set
            {
                if (value == _model.Group)
                    return;
                _model.Group = value;
                OnPropertyChanged("Group");
            }
        }

        public string Date
        {
            get { return _model.Date; }
            set
            {
                if (value == _model.Date)
                    return;
                _model.Date = value;
                OnPropertyChanged("Date");
            }
        }

        public string TrainID
        {
            get { return _model.TrainID; }
            set
            {
                if (value == _model.TrainID)
                    return;
                _model.TrainID = value;
                OnPropertyChanged("TrainID");
            }
        }

        public string AttendanceTime
        {
            get { return _model.AttendanceTime; }
            set
            {
                if (value == _model.AttendanceTime)
                    return;
                _model.AttendanceTime = value;
                OnPropertyChanged("AttendanceTime");
            }
        }


        public string AttendanceOrder
        {
            get { return _model.AttendanceOrder; }
            set
            {
                if (_model.AttendanceOrder == value.Trim())
                    return;

                _model.AttendanceOrder = value.Trim();

                OnPropertyChanged("AttendanceOrder");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (null != handler)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
    }
}
